/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.model.DistributionMsg;
import com.yami.shop.distribution.comment.service.DistributionMsgService;
import com.yami.shop.security.platform.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2019/04/01.
 */
@RestController
@RequestMapping("/distribution/distributionMsg")
@AllArgsConstructor
public class DistributionMsgController {

    private DistributionMsgService distributionMsgService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:page')")
    public ResponseEntity<IPage<DistributionMsg>> page(DistributionMsg distributionMsg, PageParam page) {
        IPage<DistributionMsg> list = distributionMsgService.getDistributionMsgsAndSysUserPage(page, distributionMsg);
        return ResponseEntity.ok(list);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:info')")
    public ResponseEntity<DistributionMsg> info(@PathVariable("id") Long id) {
        DistributionMsg distributionMsg = distributionMsgService.getById(id);
        return ResponseEntity.ok(distributionMsg);
    }

    /**
     * 保存
     */
    @PostMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:save')")
    public ResponseEntity<Void> save(@RequestBody @Valid DistributionMsg distributionMsg) {
        distributionMsg.setUpdateTime(new Date());
        distributionMsg.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionMsg.setLevel(1);
        distributionMsg.setShopId(Constant.PLATFORM_SHOP_ID);
        distributionMsgService.save(distributionMsg);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid DistributionMsg distributionMsg) {
        distributionMsg.setUpdateTime(new Date());
        distributionMsg.setModifier(SecurityUtils.getSysUser().getUserId());
        distributionMsgService.updateById(distributionMsg);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('distribution:distributionMsg:delete')")
    public ResponseEntity<Void> delete(@RequestBody List<Long> ids) {
        distributionMsgService.removeByIds(ids);
        return ResponseEntity.ok().build();
    }
}
